package com.only.common.util;

import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * @author xuxx
 * @date 2024-03-06 10:55
 */
@Slf4j
public class TestConnectUtil {


    /**
     * 连通性测试
     * @param ip
     * @param port
     * @return
     */
    public static String testConnectToServer(String ip,Integer port) {
        // 数仓查询对应节点
        try(Socket socket = new Socket()) {
            if(port == null) port = 22;
            socket.connect(new InetSocketAddress(ip,port),3000);
            if(socket.isConnected()){
                log.debug("地址：{},端口：{},连接正常",ip,port);
            }else {
                throw new Exception("无法连接："+ip+port);
            }
        }catch (Exception e){
            return "网络异常！";
        }
        return "";
    }
}
